package com.koron.css2.newProjectCenter.impl;

import cn.hutool.core.bean.BeanUtil;
import com.koron.css2.clientCenter.bean.AccountInfoBean;
import com.koron.css2.clientCenter.bean.CtmInfoBean;
import com.koron.css2.clientCenter.mapper.AccountInfoMapper;
import com.koron.css2.clientCenter.mapper.CtmInfoMapper;
import com.koron.css2.common.dto.DocDto;
import com.koron.css2.common.dto.DocFiledDto;
import com.koron.css2.common.service.AbstractExportDoc;
import com.koron.css2.newProjectCenter.bean.PjInfoBean;
import com.koron.css2.newProjectCenter.mapper.PjInfoMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.util.Tools;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.springframework.stereotype.Service;

import java.util.*;

@Slf4j
@Service
public class PjRegisterDocExport extends AbstractExportDoc {
    @Override
    public DocDto getDocText(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        try {
            Map<String,String> beanMap = (Map<String, String>) req.getData();
            if (!beanMap.containsKey("billNo")){
                throw new RuntimeException("缺少参数");
            }
            String billNo = beanMap.get("billNo");
            PjInfoMapper pjInfoMapper = factory.getMapper(PjInfoMapper.class);
            CtmInfoMapper ctmInfoMapper = factory.getMapper(CtmInfoMapper.class);
            PjInfoBean pjInfoBean = pjInfoMapper.selectByBillNo(billNo);
            Objects.requireNonNull(pjInfoBean,"参数错误,工程不存在");
            Map<String, String> idt = Tools.mapDicByCode(factory, "IDT");//证件类型
            String ctmTempId = pjInfoBean.getUserCtmTempId();
            CtmInfoBean ctmInfoBean = ctmInfoMapper.getCtmInfoById(ctmTempId, CtmInfoBean.CTMTEMP);
            AccountInfoMapper accountInfoMapper = factory.getMapper(AccountInfoMapper.class);
            String ctmName = ctmInfoBean.getCtmName();
            String certType = ctmInfoBean.getCertType();
            if(StringUtils.isNotBlank(certType)){
                certType=idt.get(certType);
            }
            String certNo = ctmInfoBean.getCertNo();

            String installAddr = pjInfoBean.getInstallAddr();
            String mobile = ctmInfoBean.getMobile();
            String agentMan = StringUtils.isNotBlank(pjInfoBean.getAgentMan())?pjInfoBean.getAgentMan():ctmName;
            String agentLinkTel= StringUtils.isNotBlank(pjInfoBean.getAgentLinkTel())?pjInfoBean.getAgentLinkTel(): mobile;
            AccountInfoBean accountInfoBean = accountInfoMapper.getAccountInfoById(pjInfoBean.getUserAccountTempId(), AccountInfoBean.ACCOUNTTEMP);
            String deliverAddr = accountInfoBean.getDeliverAddr();
            String emailAddr = accountInfoBean.getEmailAddr();

            DocFiledDto docFiledDto = new DocFiledDto();
            docFiledDto.setAgentMan(agentMan);
            docFiledDto.setAgentLinkTel(agentLinkTel);
            docFiledDto.setCertNo(certNo);
            docFiledDto.setCertType(certType);
            docFiledDto.setCtmName(ctmName);
            docFiledDto.setDeliverAddr(deliverAddr);
            docFiledDto.setFooters(Arrays.asList("三角镇客户服务中心地址：中山市三角镇月湾路6号","三角镇客户服务中心电话：0760-85542996"));
            docFiledDto.setEmailAddr(emailAddr);
            docFiledDto.setInstallAddr(installAddr);
            docFiledDto.setMobile(mobile);

//            Map<String,Object> param=new HashMap<>();
//            param.put("ctmName",ctmName);
//            param.put("certType",certType);
//            param.put("certNo",certNo);
//            param.put("installAddr",installAddr);
//            param.put("mobile",mobile);
//            param.put("agentMan",agentMan);
//            param.put("agentLinkTel",agentLinkTel);
//            param.put("deliverAddr",deliverAddr);
//            param.put("emailAddr",emailAddr);
//            param.put("list1", Arrays.asList("三角镇客户服务中心地址：中山市三角镇月湾路6号","三角镇客户服务中心电话：0760-85542996"));
//            Map<String,Object> otherMap=new HashMap<>();
//            List<DocDto> docParamList=pjInfoMapper.selectPjDocStr("register");
//            Map<String,String> docParamMap=docParamList.stream().collect(HashMap::new, (k, v) -> k.put(v.getCode(), v.getValue()),HashMap::putAll);
//            otherMap.put("footer","三角镇客户服务中心地址：中山市三角镇月湾路6号");
//            Map<String,Object> otherMap2=new HashMap<>();
//            otherMap2.put("footer","三角镇客户服务中心电话：0760-85542996");
//
//            param.put("footers",Arrays.asList(otherMap,otherMap2));
//            param.putAll(docParamMap);

            List<DocDto> docParamList=pjInfoMapper.selectPjDocStr("register");
            Map<String,String> docParamMap=docParamList.stream().collect(HashMap::new, (k, v) -> k.put(v.getCode(), v.getValue()),HashMap::putAll);
            Map<String,Object> param=new HashMap<>();
            param= BeanUtil.beanToMap(docFiledDto);
            param.putAll(docParamMap);

            DocDto docDto = new DocDto();
            docDto.setParam(param);

            docDto.setTitle("业务受理表");
            docDto.setTemplate("业务受理表.docx");
            return docDto;
        }catch (Exception e){
            log.error(e.getMessage(),e);
            throw new RuntimeException("获取工程验收文档内容失败："+e.getMessage());
        }
    }
}
